--
module("UI.Windows.LoginWin", package.seeall)

local UIUtility = require("UI.Utility")
local UIWindow = require("UI.UIWindow").UIWindow
local UIWinMgr = require("UI.UIWinMgr").UIWinMgr

LoginWin = UIWindow:new()

LoginWin.name = "LoginWin"


function LoginWin:Init()
	local t = self.Container.transform
	self.PC = UIUtility.GetChildTransform("PC", t, true)
	self.LoginButton = UIUtility.GetChildTransform("LoginButton", t, true)
	self.FBLoginButton = UIUtility.GetChildTransform("FBLoginButton", t, true)
	-----打开注册界面按钮
	self.OpenZhuCeButton = UIUtility.GetChildTransform("OpenZhuCeButton", t, true)
	-----打开找回密码界面按钮
	self.OpenFindButton = UIUtility.GetChildTransform("OpenFindButton", t, true)

	self.AccountInput = UIUtility.FindContorl('UIInput', "AccountInput", t)
	self.AccountPassWord = UIUtility.FindContorl('UIInput', "AccountPassWord", t)

	self.WxLoginMask = UIUtility.GetChildTransform("WxLoginMask", t, true)
	self.LoadingMask = UIUtility.GetChildTransform("LoadingMask", t, true)
	--版本
	self.VersionLabel = UIUtility.FindContorl('UILabel', "VersionLabel", t)

	self.qidongAni = UIUtility.GetChildTransform("qidongAni", t, true)
	self.jiazai = UIUtility.FindContorl('SpineCtrl', "jiazai", t)

	print("设备222:" .. UnityEngine.SystemInfo.deviceUniqueIdentifier)

	local hasLast = UnityEngine.PlayerPrefs.HasKey("LastPCAccount")
	if hasLast then
		self.AccountInput.value = UnityEngine.PlayerPrefs.GetString("LastPCAccount");
	end

	local hasLastps = UnityEngine.PlayerPrefs.HasKey("LastPCPassWord")
	if hasLastps then
		self.AccountPassWord.value = UnityEngine.PlayerPrefs.GetString("LastPCPassWord");
	end
	
	local VersionTool = require("VersionTool").VersionTool
	self.Version = VersionTool.getVresion()
	self.showAni = true
	self:BindEvents()
end

function LoginWin:OnShow()
	self.VersionLabel.text = self.Version
	self.LoadingMask.gameObject:SetActive(false)
	--self:TryOldSessionLogin()
	--self.qidongAni.gameObject:SetActive(false)
	self:ShowLoginAni()
end


function LoginWin:ShowLoginAni()

	if self.showAni then
		local SoundModule = require("Module.SoundModule").SoundModule
		SoundModule:SetAllEffectVolume(1)
		Debug.log("--------------ShowLoginAni----------------")
		self.qidongAni.gameObject:SetActive(true)
		SoundModule:PlaySound("loginAni_sound")
		function LoginAnidisable()
			SoundModule:StopAllSound()
			self.qidongAni.gameObject:SetActive(false)
			self.showAni = false
		end
		self.qiepaiCountDownID = CountDownMgr.Instance:CreateCountDown(5, 5, LoginAnidisable)
	end

end

function LoginWin:OnClose()

end


function LoginWin:BindEvents()
	-- function OpenPhoneJianPanWin()
	-- 	local JianPanWin = UIWinMgr:OpenWindow("JianPanWin")
	-- 	if self.AccountInput.text ~= "请输入手机号" then
	-- 		JianPanWin:SetShowLabel(self.AccountInput, self.AccountInput.text)
	-- 	else
	-- 		JianPanWin:SetShowLabel(self.AccountInput, "")
	-- 	end
	-- end
	-- UIHelper.BindUIEvnet("Click", OpenPhoneJianPanWin, self.AccountInput.gameObject)

	local LoginModule = require("Module.LoginModule").LoginModule

	function GetLoginDataCallBack(jsondata)
		Debug.LogErrorEx("jsondata.target~~~~~~~~~~~~~~~~~~~~~"..stringify(jsondata))
		if nil == jsondata then
			self.LoadingMask.gameObject:SetActive(false)
			return
		end

		if 0 ~= jsondata.code then
			Debug.LogErrorEx("遇到错误~~~")
			self.LoadingMask.gameObject:SetActive(false)
			if jsondata.code == 888 then
				Debug.log("--------------失败-------------")
			else
				UIWinMgr:OpenNotice(jsondata.msg)
			end
			return
		end

		if jsondata.updateName ~= nil then
			Debug.LogErrorEx("没有名字要修改~~~")
			local NewGameChangeNameWin = UIWinMgr:OpenWindow("NewGameChangeNameWin")
			NewGameChangeNameWin:BringToTop()
			NewGameChangeNameWin:SetTargerInfo(jsondata)
			self.LoadingMask.gameObject:SetActive(false)
		else
			if nil ~= jsondata.user then
				Debug.LogErrorEx("jsondata.target~~~~~~~~~~~~~~~~~~1111111111111111111~~~"..stringify(jsondata))
				LoginModule:Login(jsondata.user.userId, jsondata.user.sessCode, jsondata.currTime, jsondata.user.connectHost, jsondata.user.port)	
			else
				
			end
		end
	end
	self.GetLoginDataCallBack = GetLoginDataCallBack
	
	function OnLoginBtnClick()
		if not self.AccountInput.value or "" == self.AccountInput.value then
			return
		end
		
		UnityEngine.PlayerPrefs.SetString("LastPCAccount", self.AccountInput.value);

		if not self.AccountPassWord.value or "" == self.AccountPassWord.value then
			return
		end
		
		UnityEngine.PlayerPrefs.SetString("LastPCPassWord", self.AccountPassWord.value);
		--UIWinMgr:OpenMask("游戏登陆中...")
		self.LoadingMask.gameObject:SetActive(true)
		self.jiazai:DoAni("animation",true,false)
		Debug.log("self.AccountInput.value+++++++++++++++++++++++ " .. self.AccountInput.value)
		Debug.log("self.AccountPassWord.value+++++++++++++++++++++++ " .. self.AccountPassWord.value)
		LoginModule:GetLoginData(self.AccountInput.value,self.AccountPassWord.value, GetLoginDataCallBack)
	end
	
	UIHelper.BindUIEvnet("Click", OnLoginBtnClick, self.LoginButton.gameObject)


	function FaceBookLoginClick()
		local FaceBookModule = require("Module.FaceBookModule").FaceBookModule
		self.LoadingMask.gameObject:SetActive(true)

		local nouserPs = "facebook111"
		function FBLoginCallback(_success,_token)
			if _success then
				Debug.log("---------------success----")
				local _uName = FaceBookModule:GetUserName()
				local _uID = FaceBookModule:GetUserID()
				LoginModule:FBGetLoginData(_token,nouserPs,_uName,_uID, GetLoginDataCallBack) ----拉取fb登陆
				UnityEngine.PlayerPrefs.SetString("KP_FB_TOKEN",_token)
				local curFBLoginTime = os.time()
				UnityEngine.PlayerPrefs.SetString("KP_FB_TOKEN_TIME",curFBLoginTime)
			else
				Debug.log("---------------faild----")
				self.LoadingMask.gameObject:SetActive(false)
			end
			
		end
		local token = UnityEngine.PlayerPrefs.GetString("KP_FB_TOKEN")
		if token ~= nil and string.len(token) > 0 then
			local tokenTime = UnityEngine.PlayerPrefs.GetString("KP_FB_TOKEN_TIME")
			if tokenTime ~= nil and string.len(tokenTime) > 0 then
				local curSystemTime = os.time()
				local fbLoginTime = tonumber(tokenTime)
				if curSystemTime > fbLoginTime +10080 then ---每7天拉取一次
					FaceBookModule:FaceBookLogin(FBLoginCallback)
				else
					LoginModule:FBGetLoginData(token,nouserPs,"","", GetLoginDataCallBack) ----有token缓存 发token给服务器验证
				end
			else
				self.LoadingMask.gameObject:SetActive(false)
			end

		else
			FaceBookModule:FaceBookLogin(FBLoginCallback)
			
		end
		
	end
	UIHelper.BindUIEvnet("Click", FaceBookLoginClick, self.FBLoginButton.gameObject)

	

	function OpenZhuce()
		UIWinMgr:OpenWindow("ZhuCeWin")
		--UIWinMgr:OpenWindow("NewGameChangeNameWin")
	end
	UIHelper.BindUIEvnet("Click", OpenZhuce, self.OpenZhuCeButton.gameObject)

	function OpenFind()
		UIWinMgr:OpenWindow("FindWin")
	end
	UIHelper.BindUIEvnet("Click", OpenFind, self.OpenFindButton.gameObject)
end

function LoginWin:TryOldSessionLogin()
	local LoginModule = require("Module.LoginModule").LoginModule
	local hasLast = UnityEngine.PlayerPrefs.HasKey("LastPCAccount")
	local hasLastps = UnityEngine.PlayerPrefs.HasKey("LastPCPassWord")
	if hasLast and hasLastps then
		--UIWinMgr:OpenMask("游戏登陆中...")
		self.LoadingMask.gameObject:SetActive(true)
		local lastAccout = UnityEngine.PlayerPrefs.GetString("LastPCAccount")
		local lastps = UnityEngine.PlayerPrefs.GetString("LastPCPassWord")
		LoginModule:GetLoginData(lastAccout,lastps, self.GetLoginDataCallBack)
	end
end

